//
//  GYInputCodeView.m
//  Gaia
//
//  Created by weixb on 16/9/18.
//  Copyright © 2016年 weixb. All rights reserved.
//

#import "YQInputCodeView.h"
#import "WXBPaymentCodeView.h"
#import "AppDelegate.h"
#import <Masonry.h>
#import <YQTools.h>
#define KBLUECOLOR_LIGHT_3D82E5 [UIColor yq_colorWithHexString:@"#3D82E5"]

@interface YQInputCodeView () <WXBPaymentCodeViewDelegate>

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UITextField *phoneTextField;
@property (nonatomic, strong) UIButton *sendCodeBtn;
@property (nonatomic, strong) NSTimer *timer; //验证码定时器

@property (nonatomic, strong) WXBPaymentCodeView *codeView;
//协议按钮
@property (nonatomic, strong) UIButton *checkBtn;
@property (nonatomic, strong) UIButton *agreeBtn;
@property (nonatomic, strong) UIButton *protocolBtn;

@end

@implementation YQInputCodeView
{
    CGFloat cellWidth;
    CGFloat cellHeight;
    UIView *_maskView;
}

#pragma mark - initialize

#pragma mark - public
- (void)showInView:(UIView *)view {
    [view addSubview:self];
    if (_maskView == nil) {
        _maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0;
    }
    [view addSubview:_maskView];
    [view addSubview:self.contentView];
    [_contentView addSubview:self.titleLabel];
    [_contentView addSubview:self.backBtn];
    [_contentView addSubview:self.closeBtn];
    [_contentView addSubview:self.phoneTextField];
    [_contentView addSubview:self.sendCodeBtn];
    
    [_contentView addSubview:self.codeView];
    [_contentView addSubview:self.checkBtn];
    [_contentView addSubview:self.agreeBtn];
    [_contentView addSubview:self.protocolBtn];
    
    [self layoutSubviews];
    [UIView animateWithDuration:0.4 animations:^{
        _maskView.alpha = 0.3;
        _contentView.alpha = 1;
    }];
}

#pragma mark - private
/**
 *  发送验证码之后等待若干时间
 */
static NSInteger seconds = 60;
- (void)respondstoTimer:(NSTimer *)sender {
    _sendCodeBtn.enabled = NO;
    NSString *btnTitle = [NSString stringWithFormat:@"%ld秒后重发",(long)seconds--];
    if(seconds < 0){
        _sendCodeBtn.enabled = YES;
        [_timer invalidate];
        _timer = nil;
        btnTitle = @"重发验证码";
    }
    UIColor *color_3d82e5 = [UIColor yq_colorWithHexString:@"#3D82E5"];
    UIColor *color = _sendCodeBtn.enabled?color_3d82e5:[UIColor lightGrayColor];
    
    [_sendCodeBtn  setTitle:btnTitle forState:UIControlStateNormal];
    [_sendCodeBtn setTitleColor:color forState:UIControlStateNormal];
    
    //    UILabel *label = [[UILabel alloc] initWithFrame:_sendCodeBtn.bounds];
    //    label.text = btnTitle;
    //    [_sendCodeBtn addSubview:label];
}
- (void)startTimer {
    [_sendCodeBtn setEnabled:NO];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(respondstoTimer:) userInfo:nil repeats:YES];
}
- (void)dismiss {
    [UIView animateWithDuration:0.4 animations:^{
        _maskView.alpha = 0;
        _contentView.alpha = 0;
    } completion:^(BOOL finished) {
        [_maskView removeFromSuperview];
        [_contentView removeFromSuperview];
        [self removeFromSuperview];
    }];
}
#pragma mark - target
- (void)clickBackBtn {
    NSLog(@"点击了返回");
    [_codeView.TF resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(backToLoginView)]) {
        [self.delegate backToLoginView];
    }
}
- (void)clickCloseBtn:(UIButton *)sender {
    NSLog(@"点击了关闭");
//    exit(0);
    if ([self.delegate respondsToSelector:@selector(dismiss)]) {
        [self.delegate dismiss];
    }
}
- (void)clickSendCodeBtn:(UIButton *)sender {
    if (!_checkBtn.selected) {
//        [SVProgressHUD showInfoWithStatus:@"请先同意用户协议"];
        return;
    }
    //发送验证码请求
    
}
- (void)clickCheckBtn:(UIButton *)sender {
    _checkBtn.selected = !_checkBtn.selected;
    _codeView.TF.enabled = _checkBtn.selected;
}
- (void)clickProtocolBtn:(UIButton *)sender {
    
}
#pragma mark - <WXBPaymentCodeViewDelegate>
- (void)finishedPaymentCode:(NSString *)paymentCode {
    [_codeView.TF resignFirstResponder];
    
}

#pragma mark - 设置约束
- (void)layoutSubviews {
    [super layoutSubviews];
    cellWidth = self.bounds.size.width;
    cellHeight = self.bounds.size.height;
    
    CGFloat spacing = 20;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(18);
        make.centerX.equalTo(_contentView);
    }];
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel);
        make.left.mas_equalTo(spacing);
    }];
    [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel);
        make.right.mas_equalTo(-spacing);
    }];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_contentView).offset(-20);
        make.left.mas_equalTo(spacing);
        make.width.mas_equalTo(_contentView.width * .55f);
        make.height.mas_equalTo(40*YQHorizentalMutiplier());
    }];
    [_sendCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_phoneTextField);
        make.left.equalTo(_phoneTextField.mas_right).offset(-1);
        make.right.mas_equalTo(-spacing);
        make.height.equalTo(_phoneTextField);
    }];
    [_codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_contentView);
        make.top.equalTo(_phoneTextField.mas_bottom).offset(17);
        make.bottom.equalTo(_agreeBtn.mas_top).offset(-8);
    }];
    [_checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_agreeBtn.mas_left).offset(-3);
        make.centerY.equalTo(_agreeBtn);
        make.width.height.mas_equalTo(18);
    }];
    [_agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_contentView.mas_centerX).offset(-8);
        make.bottom.mas_equalTo(-8);
    }];
    [_protocolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_agreeBtn.mas_right);
        make.top.equalTo(_agreeBtn);
    }];
}
#pragma mark - getter

- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.bounds = CGRectMake(0, 0, SCREEN_WIDTH * .9f, 200 * YQHorizentalMutiplier());
        _contentView.center = CGPointMake(SCREEN_WIDTH/2, SCREENH_HEIGHT/2);
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 5;
        _contentView.layer.masksToBounds = YES;
        _contentView.alpha = 0.5;
    }
    return _contentView;
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"输入验证码";
    }
    return _titleLabel;
}
- (UIButton *)backBtn {
    if (_backBtn == nil) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_backBtn setTitle:@"返回" forState:UIControlStateNormal];
        [_backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}
- (UIButton *)closeBtn {
    if (_closeBtn == nil) {
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
        [_closeBtn setImage:[UIImage imageNamed:@"icon_close"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(clickCloseBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}
- (UITextField *)phoneTextField {
    if (_phoneTextField == nil) {
        _phoneTextField = [[UITextField alloc] init];
        _phoneTextField.placeholder = @"手机号";
        _phoneTextField.enabled = NO;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 20)];
        _phoneTextField.leftView = view;
        _phoneTextField.leftViewMode = UITextFieldViewModeAlways;
        FUNC_BorderRadius(_phoneTextField, 3, .5f, [UIColor yq_colorWithHexString:@"999999"]);
    }
    return _phoneTextField;
}
- (UIButton *)sendCodeBtn {
    if (_sendCodeBtn == nil) {
         _sendCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendCodeBtn.backgroundColor = [UIColor yq_colorWithHexString:@"F8F8F8"];
        [_sendCodeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        [_sendCodeBtn setTitleColor:KBLUECOLOR_LIGHT_3D82E5 forState:UIControlStateNormal];
        _sendCodeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_sendCodeBtn addTarget:self action:@selector(clickSendCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
        FUNC_BorderRadius(_sendCodeBtn, 3, .5f, [UIColor yq_colorWithHexString:@"999999"]);
    }
    return _sendCodeBtn;
}

- (WXBPaymentCodeView *)codeView {
	if (_codeView == nil) {
        _codeView = [[WXBPaymentCodeView alloc] init];
        _codeView.delegate = self;
	}
	return _codeView;
}
- (UIButton *)checkBtn {
	if (_checkBtn == nil) {
        _checkBtn = [[UIButton alloc] init];
        [_checkBtn setImage:[UIImage imageNamed:@"icon_n_check"] forState:UIControlStateNormal];
        [_checkBtn setImage:[UIImage imageNamed:@"icon_check"] forState:UIControlStateSelected];
        [_checkBtn addTarget:self action:@selector(clickCheckBtn:) forControlEvents:UIControlEventTouchUpInside];
        _checkBtn.selected = YES;
	}
	return _checkBtn;
}
- (UIButton *)agreeBtn {
	if (_agreeBtn == nil) {
        _agreeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_agreeBtn setTitle:@"我已阅读并同意" forState:UIControlStateNormal];
        [_agreeBtn setTitleColor:KBLUECOLOR_LIGHT_3D82E5 forState:UIControlStateNormal];
        [_agreeBtn addTarget:self action:@selector(clickCheckBtn:) forControlEvents:UIControlEventTouchUpInside];
//        _agreeBtn.enabled = NO;
	}
	return _agreeBtn;
}
- (UIButton *)protocolBtn {
	if (_protocolBtn == nil) {
        _protocolBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_protocolBtn setTitle:@"《用户协议》" forState:UIControlStateNormal];
        [_protocolBtn setTitleColor:KBLUECOLOR_LIGHT_3D82E5 forState:UIControlStateNormal];
        [_protocolBtn addTarget:self action:@selector(clickProtocolBtn:) forControlEvents:UIControlEventTouchUpInside];
	}
	return _protocolBtn;
}
#pragma mark - setter
- (void)setPhoneNumber:(NSString *)phoneNumber {
    _phoneNumber = phoneNumber;
    _phoneTextField.text = phoneNumber;
}


@end




